<?php
include 'checkLogin.php';

@$id = $_REQUEST['id'];
@$stockType = $_REQUEST['stockType'];

@$inStock = $_REQUEST['inStock'];
@$outStock = $_REQUEST['outStock'];

if($inStock < 0||$outStock < 0){					//数目小于0
	$mes = '操作错误，检查输入的数量';
	include 'updateStock.php';
	die;
}
include 'db/li.php';

@$stock = dbSelect('select stock from goods where id=:id',array('id'=>$id));

if(!empty($inStock)){
	@$flog = dbUpdate("update goods set stock = stock + $inStock,sellStatus='上架' where id=:id",array('id'=>$id));//加库存-还原状态
	flog($flog);
}
if(!empty($outStock)){
	if($outStock + 5 > $stock['stock']){		//判断保底量
		$mes = '库存量不能低于5件';
		include 'updateStock.php';
		die;
	}
	@$flog = dbUpdate("update goods set stock = stock - $outStock where id=:id",array('id'=>$id));	//减库存
	flog($flog);
	@$stock = dbSelect('select stock from goods where id=:id',array('id'=>$id));
	if($stock['stock'] <= 5){									//小于5件 改销售状态
		$flog = dbUpdate("update goods set sellStatus='缺货' where id=:id",array('id'=>$id));
		if(!$flog){
			$mes = '修改销售状态失败';
			include 'index.php';
			die;
		}
	}
}

function flog($flog){
	if(!$flog){
		$mes = '操作失败';
		include 'updateStock.php';
		die;
	}
}
header('Refresh:0;url=goodsList.php?listType=stock');